package org.jeecg.modules.cascv.image.tools;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.HexUtil;
import com.alibaba.fastjson.JSONObject;

import java.io.RandomAccessFile;
import java.util.concurrent.TimeUnit;

/**
 * @author;作者
 * @Date 2023/3/17 16:26
 * @Description :  无容器数据生成
 * @Version
 */
public class WurongqiDataProcess {

    public static void main(String[] args) throws Exception {
    RandomAccessFile randomAccessFile = new RandomAccessFile("D:\\pc_workspace\\wurongqi\\" +
            "pa_ue_tgth_0b2_apd_0090_003c_6d7d_sd00_20221110182238_20221110182310_20221110175958_re00_000_712_cg.dat", "r");
    byte[] bytes = new byte[2024];

    while(true){
        int read = randomAccessFile.read(bytes);
        if(read <= 0){
            break;
        }

        String str = HexUtil.encodeHexStr(bytes);

        Integer startAndPackageNo = Integer.parseUnsignedInt(str.substring(12, 16), 16);
        // 1111 1111 1111 1111
        // 0011 1111 1111 1111
        System.out.println("startAndPackageNo = " + startAndPackageNo);

        int startFlag = startAndPackageNo >>> 14;

        int packageNo = startAndPackageNo & 0x3fff;

        System.out.println("startAndPackageNo = " + startAndPackageNo);

        String startFlagStr = Integer.toBinaryString(startFlag);
        if(startFlag==1||startFlag==0){
            startFlagStr="0"+startFlagStr;
        }
        Long second = Long.parseUnsignedLong(str.substring(16, 22), 16);

        Long subSecond = Long.parseUnsignedLong(str.substring(22, 26), 16);
        String frameStr = str.substring(32, 4044);//32

        System.out.println(second + "," + subSecond + "," + startFlag + "," + packageNo);
        String time = toDayHoursMinSec(second);
        System.out.println("time = " + time);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("tableName", "TGTHYY0401_090H_31_CMPRAPP6");
        JSONObject content = new JSONObject();
        jsonObject.put("Content", content);

        content.put("A1_CMPR_MICU_APP6DATA_6_7_2",packageNo+";"+packageNo+";包序号");//序列标志
        content.put("A1_CMPR_MICU_APP6DATA_8_11_1", second+";"+time+";时间码秒计数");//秒值
        content.put("A1_CMPR_MICU_APP6DATA_12_13_1",subSecond+";"+subSecond+";时间码微秒计数");//微秒值
        content.put("A1_CMPR_MICU_APP6DATA_6_7_1",startFlagStr+"B;"+startFlagStr+"B;序列标识");//序列标志
        content.put("A1_CMPR_MICU_APP6DATA_16_2021_1", "null;"+"0x"+frameStr+";null;null");//数据帧



        FileUtil.appendUtf8String(jsonObject.toString() + System.lineSeparator(),
                "D:\\pc_workspace\\wurongqi2\\"+
                        "TGTHYY0401_090H_31_CMPRAPP238.txt");
        //封装json数据
        //推送kafka数据
    }
}
    /**
     * 秒数转天时分秒
     */
    public static String toDayHoursMinSec(Long time){
        long day = TimeUnit.SECONDS.toDays(time);
        long hours = TimeUnit.SECONDS.toHours(time) - TimeUnit.DAYS.toHours(TimeUnit.SECONDS.toDays(time));
        long minutes = TimeUnit.SECONDS.toMinutes(time) - TimeUnit.HOURS.toMinutes(TimeUnit.SECONDS.toHours(time));
        long seconds = TimeUnit.SECONDS.toSeconds(time) - TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(time));
        // 172800+14400+1720
        return day+":"+hours+":"+minutes+":"+seconds;
    }
}
